package com.appbee.cm.client.regionunit.presenter;

import javax.inject.Inject;
import javax.inject.Provider;

import com.appbee.cm.client.nav.presenter.NavControl;
import com.appbee.cm.client.nav.presenter.NavItemWrap;
import com.appbee.core.client.place.INamePlace;
import com.google.gwt.activity.shared.Activity;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.place.shared.Place;

public class ModulActivityMapper implements ActivityMapper
{
	private final NavControl navControl;
	private String id;
	private final Provider<RegionActivity> regionActivitProvider;

	@Inject
	public ModulActivityMapper(	NavControl navControl,
								Provider<RegionActivity> regionActivitProvider)
	{
		this.navControl = navControl;
		this.regionActivitProvider = regionActivitProvider;

	}

	@Override
	public Activity getActivity(Place place)
	{
		// Return null if place is not INamePlace
		if (!(place instanceof INamePlace))
			return null;

		// Find navigation item
		INamePlace namePlace = (INamePlace) place;
		NavItemWrap navItem = navControl.getByPlace(namePlace);
		if (navItem == null)
			return null;

		RegionActivity modulActivity = regionActivitProvider.get();
		modulActivity.setCurrentNavItem(navItem.getWrappedItem());
		modulActivity.setRegionId(id);
		modulActivity.setPlace(place);
		return modulActivity;
	}

	public void setId(String id)
	{
		this.id = id;
	}

}
